package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 添加套餐
     *
     * @param setmealDto
     * @return
     */
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal表，进行添加操作
        this.save(setmealDto);
        //保存套餐和菜品的关联信息，操作setmeal_dish表，进行insert操作
        Long id = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        setmealDishService.saveBatch(setmealDishes);
    }


    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        //删除基本信息
        //判断状态是否为禁用
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(ids.size() != 0, Setmeal::getId, ids).eq(Setmeal::getStatus, 1);
        int count = count(lqw);
        if (count > 0) {
            throw new CustomException("删除套餐中含有启售产品，请先禁售");
        }
        this.removeByIds(ids);
        //删除菜品信息

        LambdaQueryWrapper<SetmealDish> setmealdishlqw = new LambdaQueryWrapper<>();
        setmealdishlqw.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(setmealdishlqw);


    }

    /**
     * 根据id查询菜品和对应的口味信息
     *
     * @return
     */
    @Override
    public SetmealDto getByidWith(Long id) {
        //查询套餐的基本信息
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        //查询套餐菜品的关联信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);
        //设置套餐菜品关联信息
        setmealDto.setSetmealDishes(setmealDishes);

        return setmealDto;
    }

    /**
     * 修改套餐
     *
     * @param setmealDto
     * @return
     */
    public void updateAll(SetmealDto setmealDto) {
        //更新基本信息
        this.updateById(setmealDto);
        //清空套餐菜品关联信息，然后进行添加
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        //清理当前套餐菜品关联信息
        setmealDishService.remove(wrapper);
        //添加套餐菜品关联信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }
}
